package com.the9tcat.deliverycoming.data;

import java.util.Calendar;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.RingtoneManager;
import android.net.Uri;

/**
 * The Class Configure.
 */
public class Configure {
	
	/** The m instance. */
	private static Configure mInstance = null;

	/** The m preferences. */
	private static SharedPreferences mPreferences;

	/** The m editor. */
	private static Editor mEditor;
	
	/** Return track list only unsigned */
	public static final int MODE_UNSIGNED_TRACKLIST = 0;
	
	/** Return all track list */
	public static final int MODE_ALL_TRACKLIST = 1;

	/** The m context. */
	private Context mContext;

	/** The Constant UPDATE_FREQ. */
	public static final long[] UPDATE_FREQ = { 30 * 60 * 1000,
			60 * 60 * 1000, 2 * 60 * 60 * 1000, 4 * 60 * 60 * 1000,
			6 * 60 * 60 * 1000, 12 * 60 * 60 * 1000 };

	/**
	 * Instantiates a new configure.
	 *
	 * @param con the con
	 */
	public Configure(Context con) {
		mContext = con;
		mPreferences = con.getSharedPreferences("setting", 0);
		mEditor = mPreferences.edit();
	}

	/**
	 * Gets the single instance of Configure.
	 *
	 * @param con the con
	 * @return single instance of Configure
	 */
	public static synchronized Configure getInstance(Context con) {

		if (mInstance == null) {
			mInstance = new Configure(con);
		}

		return mInstance;
	}

	/**
	 * Enable sound.
	 *
	 * @param b the b
	 */
	public void enableSound(boolean b) {
		mEditor.putBoolean("sound", b);
		mEditor.commit();
	}

	/**
	 * Checks if is sound enabled.
	 *
	 * @return true, if is sound enabled
	 */
	public boolean isSoundEnabled() {
		return mPreferences.getBoolean("sound", true);
	}

	/**
	 * Sets the sound.
	 *
	 * @param index the new sound
	 */
	public void setSound(int index) {
		mEditor.putInt("sound_type", index);
		mEditor.commit();
	}

	/**
	 * Gets the sound index.
	 * for setting page.
	 *
	 * @return the sound index
	 */
	public int getSoundIndex() {
		return mPreferences.getInt("sound_type", -1);
	}

	/**
	 * Gets the sound uri.
	 * for notification
	 *
	 * @return the sound uri
	 */
	public Uri getSoundUri() {
		RingtoneManager rm = new RingtoneManager(mContext);
		rm.setType(RingtoneManager.TYPE_ALARM | RingtoneManager.TYPE_NOTIFICATION);
		int index = getSoundIndex();
		if (index == -1) {
		    return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM | RingtoneManager.TYPE_NOTIFICATION);
		} else {
		    return rm.getRingtoneUri(index);
		}
	}

	/**
	 * Enable vebrate.
	 *
	 * @param b the b
	 */
	public void enableVebrate(boolean b) {
		mEditor.putBoolean("vebrate", b);
		mEditor.commit();
	}

	/**
	 * Checks if is vebrate enabled.
	 *
	 * @return true, if is vebrate enabled
	 */
	public boolean isVebrateEnabled() {
		return mPreferences.getBoolean("vebrate", true);
	}

	/**
	 * Enable led.
	 *
	 * @param b the b
	 */
	public void enableLed(boolean b) {
		mEditor.putBoolean("led", b);
		mEditor.commit();
	}

	/**
	 * Checks if is led enabled.
	 *
	 * @return true, if is led enabled
	 */
	public boolean isLedEnabled() {
		return mPreferences.getBoolean("led", true);
	}

	/**
	 * Sets the update freq.
	 *
	 * @param index the new update freq
	 */
	public void setUpdateFreq(int index) {
		mEditor.putInt("update_freq", index);
		mEditor.commit();
	}
	
	public void setAdPosition(int index) {
		mEditor.putInt("ad_position", index);
		mEditor.commit();
	}

	/**
	 * Gets the update freq index.
	 * for setting page
	 *
	 * @return the update freq index
	 */
	public int getUpdateFreqIndex() {
		return mPreferences.getInt("update_freq", 1);
	}
	
	
	public int getAdPositionIndex() {
		return mPreferences.getInt("ad_position", 0);
	}
	
	/**
	 * Gets the update freq by ms.
	 * for general use
	 *
	 * @return the update freq
	 */
	public long getUpdateFreq() {
	    return Configure.UPDATE_FREQ[getUpdateFreqIndex()];
	}
	
	public int getAdPostion() {
	    return getAdPositionIndex();
	}
	
	/**
	 * Enable dnd.
	 *
	 * @param b the b
	 */
	public void enableDnd(boolean b) {
		mEditor.putBoolean("dnd_enable", b);
		mEditor.commit();
	}
	
	/**
	 * Checks if is dnd enabled.
	 *
	 * @return true, if is dnd enabled
	 */
	public boolean isDndEnabled() {
		return mPreferences.getBoolean("dnd_enable", true);
	}
	
	/**
	 * Sets the dnd start.
	 *
	 * @param hour the new dnd start
	 */
	public void setDndStart(int hour) {
		mEditor.putInt("dnd_start", hour);
		mEditor.commit();
	}
	
	/**
	 * Sets the dnd end.
	 *
	 * @param hour the new dnd end
	 */
	public void setDndEnd(int hour) {
		mEditor.putInt("dnd_end", hour);
		mEditor.commit();
	}
	
	/**
	 * Gets the dnd start.
	 *
	 * @return the dnd start
	 */
	public int getDndStart() {
		return mPreferences.getInt("dnd_start", 21);
	}
	
	/**
	 * Gets the dnd end.
	 *
	 * @return the dnd end
	 */
	public int getDndEnd() {
		return mPreferences.getInt("dnd_end", 9);
	}
	
	/**
	 * 是否在免打扰状态
	 * 
	 * @return
	 */
	public boolean isInDndTime() {
		if (isDndEnabled()) {
            Calendar can = Calendar.getInstance();
            int hour = can.get(Calendar.HOUR_OF_DAY);
            int dndStart = getDndStart();
            int dndEnd = getDndEnd();
            if (hour < dndEnd || hour > dndStart) {
                // 在免打扰时间段内
                return true;
            }
        }
		return false;
	}
}
